﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Administrator
{
    public partial class ucUser : UserControl
    {
        public Boolean UserChangePassword = false;
        public String UserCaption = "";
        public String UserNewPassword = "";
        public String UserConfirmPassword = "";
        public Int32 UserAccessID = -1;
        private Boolean LoadSetting = false;        
        private Int32[] AccessIdx = new Int32[0];
        
        public ucUser()
        {
            InitializeComponent();
        }

        //Заполнение полей структуры и формы
        public void FillFields(String Caption, Int32 AccessID)
        {
            Int32 i;
            
            UserCaption = Caption;            
            UserAccessID = AccessID;

            LoadSetting = true;

            tbCaption.Text = UserCaption;
            AccessIdx = new Int32[0];
            for (i = 0; i < cMain.AccessList.Length; i++)
            {
                if (!cMain.AccessList[i].ItemAction.Deleted)
                {
                    Array.Resize(ref AccessIdx, AccessIdx.Length + 1);
                    cbAccess.Items.Add(cMain.AccessList[i].Caption);
                    AccessIdx[AccessIdx.Length - 1] = i;
                    if (UserAccessID == cMain.AccessList[i].ID)
                        cbAccess.SelectedIndex = cbAccess.Items.Count - 1;
                }
            }
            chbChangePasword.Checked = UserChangePassword;
            lbPassword.Enabled = false;
            lbConfirmPassword.Enabled = false;
            tbPassword.Enabled = false;
            tbConfirmPassword.Enabled = false;
            tbPassword.Text = "";
            tbConfirmPassword.Text = "";
            LoadSetting = false;
        }
        private void chbChangePasword_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadSetting)
            {
                UserChangePassword = chbChangePasword.Checked;
                if (UserChangePassword)
                {
                    lbPassword.Enabled = true;
                    lbConfirmPassword.Enabled = true;
                    tbPassword.Enabled = true;
                    tbConfirmPassword.Enabled = true;
                    tbPassword.Text = "";
                    tbConfirmPassword.Text = "";
                }
                else
                {
                    lbPassword.Enabled = false;
                    lbConfirmPassword.Enabled = false;
                    tbPassword.Enabled = false;
                    tbConfirmPassword.Enabled = false;
                    tbPassword.Text = "";
                    tbConfirmPassword.Text = "";
                }
            }
        }

        private void tbCaption_TextChanged(object sender, EventArgs e)
        {
            if (sender == tbCaption) UserCaption = ((TextBox)sender).Text;
            if (sender == tbPassword) UserNewPassword = ((TextBox)sender).Text;
            if (sender == tbConfirmPassword) UserConfirmPassword = ((TextBox)sender).Text;
        }
        private void cbAccess_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 Idx = -1;

            if (!LoadSetting)
            {
                if (sender == cbAccess)
                {
                    Idx = cbAccess.SelectedIndex;
                    if ((Idx >= 0) && (Idx < AccessIdx.Length))
                        UserAccessID = cMain.AccessList[AccessIdx[Idx]].ID;
                    else UserAccessID = -1;                    
                }
            }
        }
    }
}
